﻿
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>



#define  I2C_W_DATA  100
#define	 I2C_R_DATA  101


/*
 * ./at24c02_zmq_test /dev/zmq_at24c02  w  data_address  data
 * ./at24c02_zmq_test /dev/zmq_at24c02  r  data_address  
 */

int main(int argc, char **argv)
{

	int fd, ret;
	int data[2];


	if (argc != 4 && argc != 5)
	{
		printf("Usage: %s /dev/zmq_at24c02 w <data_address>  <data> \n", argv[0]);
		printf("       %s /dev/zmq_at24c02 r <data_address> \n", argv[0]);
		return -1;
	}
	
	fd = open(argv[1], O_RDWR);
	if (fd == -1)
	{
		printf("Open %s err.\n", argv[1]);
		return -1;
	}
	
	if (argv[2][0] == 'w')
	{
		data[0] = strtoul(argv[3], NULL, 0);
		data[1] = strtoul(argv[4], NULL, 0);
		ioctl(fd, I2C_W_DATA, data);
	}

	if (argv[2][0] == 'r')
	{
		data[0] = strtoul(argv[3], NULL, 0);
		ioctl(fd, I2C_R_DATA, data);
		printf("Read address : %x ,get data: %x\n", data[0], data[1]);
	}

	close(fd);
	return 0;
}


